package com.vgulu.server.common.mongodb;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.util.*;

/**
 * Created by wangchunsee on 2016/10/25.
 */
public class MongoDbFactory {
    private static MongoClient client;
//    private static Map<String,MongoDatabase> database=new HashMap<>();
    static {
        MongoClientOptions settings = MongoClientOptions.builder()
                .socketKeepAlive(true)
                .codecRegistry(com.mongodb.MongoClient.getDefaultCodecRegistry()).build();
        MongoCredential credential = MongoCredential.createMongoCRCredential("benzhuo", "admin", "Benzhuo3#".toCharArray());
        client = new MongoClient(new ServerAddress("sch.gudonger.com",26518),Arrays.asList(credential),settings);
//        database = client.getDatabase("yydemo");

    }

    public static MongoDatabase instance(String db){
        return client.getDatabase(db);
    }

    public static MongoCollection<Document> getCollection(String db,String col){
        return instance(db).getCollection(col);
    }
}
